package com.chenliang.controller;

import com.chenliang.domain.Product;
import com.chenliang.service.IService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import java.util.List;

/**
 * @ProjectName: mavenSSM
 * @Package: com.chenliang.controller
 * 作用：
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IService service;

//产品管理
    @RequestMapping("/findAll")
    public String findall(Model model,
      @RequestParam(name="page",required = true,defaultValue = "1")Integer page,
      @RequestParam(name = "pageSize",required = true,defaultValue = "3")Integer pageSize){

        List<Product> product = service.findall(page,pageSize);
//        把数据存入分页对象中
        PageInfo pageInfo=new PageInfo(product);
        model.addAttribute("pageInfo",pageInfo);
        return "product-list";
    }

//产品添加

    @RequestMapping("/add")
    public String add(Product product){
         service.addProduct(product);

        return "redirect:/product/findAll";
    }

//    产品删除
    @RequestMapping("/delMethod")
public String delMethod(String[] ids){
        if (ids!=null){
            service.delMethod(ids);
        }
        return "redirect:/product/findAll";

}

}
